package com.aios.util;

import java.util.HashMap;
import java.util.Map;

import com.aios.entity.Switch;
import com.aios.exception.AiosStatusUpdateException;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

public class JsonConvertor {

	public static final String STATUS = "status";
	public static final String STATE = "state";
	public static final String SUCCESS = "success";
	public static final String ERROR = "error";
	public static final String CODE = "code";
	public static final String MESSAGE = "message";
	public static final String VALUE = "value";
	
	public static Map<String, String> convertAiosStateResponse(String json) throws AiosStatusUpdateException {

		JSONObject jsonValues = (JSONObject) JSONSerializer.toJSON( json );

		if (jsonValues.containsKey(STATE)) {

			 Map<String, String> statusMap = new HashMap<String,String>(Switch.NUMBER_OF_SWITCHES);
			 
			 JSONObject state = jsonValues.getJSONObject(STATE);

			 for(String key : AiosAccessUtil.BUTTON_IDS) {
				 statusMap.put(key, state.getString(key));
			 }

			 return statusMap;
		}

		throw new AiosStatusUpdateException("Could not update the status, please check the device");

	}

}
